package com.javalong.affairs.module.notifything.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.MapKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.ui.SimplePullRefreshList;
import com.javalong.affairs.base.ui.SimplePullRefreshList.OnLoadSuccess;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnRightTextClick;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.ListUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.notifything.adapter.ReceiverAdapter;
import com.javalong.affairs.module.notifything.adapter.ReceiverAdapter.Type;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ContentView(R.layout.activity_receiver)
public class NewReceiverActivity extends UIHandlerActivity implements OnRightTextClick {
    @ViewInject(R.id.ui_toptitle)
    private TopTitle topTitle;
    @ViewInject(R.id.lv_receiver)
    private SimplePullRefreshList lv_receiver;
    @ViewInject(R.id.et_search)
    private EditText et_search;
    @ViewInject(R.id.bt_search)
    private Button bt_search;
    private ReceiverAdapter adapter;
    private int type;
    private List<User> userList;
    private User selectAllUser;
    private UIHandler uiHandler;
    private int flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHandler = ServiceLocator.GetInstance().getInstance(UIHandler.class);
        ViewUtils.inject(this);
        topTitle.setOnLeftDrawableClick(this);
        userList = AllData.GetInstance().appendListData(
                R.string.data_receiver_users, new ArrayList<User>());
        adapter = new ReceiverAdapter(this, userList, Type.RECEIVER);
        lv_receiver.setAdapter(adapter);
        lv_receiver.initData(getMapData());
        lv_receiver.refresh();
        topTitle.setOnRightTextClick(this);
        selectAllUser = new User();
        selectAllUser.id = -1;
        selectAllUser.name = "全选";
        lv_receiver.setOnLoadSuccess(new OnLoadSuccess() {
            @Override
            public void onSuccess() {
                User user = ListUtils.getSafeItem(userList, 0);
                if (user != null && user.id == -1) {
                    return;
                } else {
                    userList.add(0, selectAllUser);
                }
            }
        });
        flag = getIntent().getIntExtra("flag", 0);
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            topTitle.setTitle("审核人列表");
        }
        bt_search.setOnClickListener(this);
    }

    private Map<String, Object> getMapData() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(MapKey.ACTION_NAME, "SendMessage");
        param.put(MapKey.METHOD_NAME, "outList_ableMessage");
        param.put(MapKey.DATA_RESID, R.string.data_receiver_users);
        param.put(MapKey.DATA_TYPE, DataType.LIST_USER);
        return param;
    }

    @Override
    public void rightTextClick() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.USERLIST,
                (Serializable) adapter.getSelectedUsers());
        uiHandler.postCallback(flag, bundle);
        finish();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_search:
                searchKey();
                break;
            default:
                break;
        }
    }

    private void searchKey() {
        String key = et_search.getText().toString();
        String url = UrlUtils.getUrl("SendMessage", "outList_userList_byName");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("name", et_search.getText().toString());
        HttpUtils.post(url, param,
                new BaseResponseListener(DataType.LIST_USER) {
                    @Override
                    public void onSuccess(Object obj) {
                        super.onSuccess(obj);
                        List<User> tempUserList = (List<User>) obj;
                        userList.clear();
                        userList.addAll(tempUserList);
                        userList.add(0, selectAllUser);
                        adapter.notifyDataSetChanged();
                    }
                });

    }
}
